<?php require('connections/con_gl.php'); ?>
<?php
session_start();
cekAkses($_SESSION[akses],"L-8");
?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_data = 30;
$pageNum_data = 0;

$startRow_data = $pageNum_data * $maxRows_data;
mysql_select_db($database_con_gl, $con_gl);
$query_data = "SELECT sum(gl_trans.total) as total, gl_rtrans.kontak, gl_kontak.nama FROM gl_rtrans, gl_trans, gl_kontak WHERE gl_kontak.id=gl_rtrans.kontak AND gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.pos='K' AND gl_trans.x_ref is NULL AND gl_trans.barang='$_GET[barang]'";
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_data .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
if($_GET[mulai]<>'' AND $_GET[sampai]<>'') {
$query_data .=" AND gl_rtrans.tgl between '$_POST[mulai]' AND '$_POST[sampai]'";
}
if($_GET[agen]<>'' AND $_GET[agen]<>'ALL') {
$query_data .=" AND gl_rtrans.kontak='$_GET[agen]'";
}
$query_data .=" group by gl_kontak.nama asc";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);

mysql_select_db($database_con_gl, $con_gl);
$query_agen = "SELECT gl_barang.id, gl_barang.barang, gl_kelompok.id as kel_id FROM gl_barang, gl_kelompok where gl_barang.kelompok = gl_kelompok.id AND gl_kelompok.jenis=2 ORDER BY barang ASC";
$agen = mysql_query($query_agen, $con_gl) or die(mysql_error());
$row_agen = mysql_fetch_assoc($agen);
$totalRows_agen = mysql_num_rows($agen);

mysql_select_db($database_con_gl, $con_gl);
$query_lagen = "select id, nama from gl_kontak where type='Customer'";
$lagen = mysql_query($query_lagen, $con_gl);
$row_lagen = mysql_fetch_assoc($lagen);
$totalRows_lagen = mysql_num_rows($lagen);

$colname_getagen = "-1";
if (isset($_GET['barang'])) {
  $colname_getagen = (get_magic_quotes_gpc()) ? $_GET['barang'] : addslashes($_GET['barang']);
}
mysql_select_db($database_con_gl, $con_gl);
$query_getagen = sprintf("SELECT barang FROM gl_barang WHERE id = %s", $colname_getagen);
$getagen = mysql_query($query_getagen, $con_gl) or die(mysql_error());
$row_getagen = mysql_fetch_assoc($getagen);
$totalRows_getagen = mysql_num_rows($getagen);

?>
<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="js/ui.datepicker.js"></script>
<script type="text/javascript">
<!--
$(function()
      {
        $('.calendar').datepicker({
            appendText : "",
            dateFormat : 'yy/mm/dd'
          });
      });
</script>
<h1>REKAP SIRKULASI SEWA </h1>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 
  <tr align="right">
    <td colspan="9"><form id="search" name="search" method="post" action="">
      <label>
        <select name="agen" id="agen">
          <option value="ALL">ALL Customer</option>
          <?php do { ?>
          <option value="<?php echo $row_lagen['id']?>" <?php if($row_lagen['id']==$_GET['agen']) { ?>selected="selected"<?php } ?>><?php echo $row_lagen['nama']?></option>
           <?php
} while ($row_lagen = mysql_fetch_assoc($lagen));
  $rows = mysql_num_rows($lagen);
  if($rows > 0) {
      mysql_data_seek($lagen, 0);
	  $row_lagen = mysql_fetch_assoc($lagen);
  }
?>
        </select>
      </label>
      <label>
      <select name="barang" id="barang">
        <?php
do {  
?>
        <option value="<?php echo $row_agen['id']?>"><?php echo $row_agen['barang']?></option>
        <?php
} while ($row_agen = mysql_fetch_assoc($agen));
  $rows = mysql_num_rows($agen);
  if($rows > 0) {
      mysql_data_seek($agen, 0);
	  $row_agen = mysql_fetch_assoc($agen);
  }
?>
      </select>
      </label>
<label>
<input name="mulai" type="text" id="mulai" value="<?php echo $_REQUEST['mulai'];?>" size="10" maxlength="15" class="calendar";/>
      </label>s/d
	  <label>
      <input name="sampai" type="text" id="sampai" value="<?php echo $_REQUEST['sampai'];?>" size="10" maxlength="15" class="calendar"/>
	  </label>
      <label>
      <input name="Search" type="submit" id="Search" value="Search" />
      </label>
    </form>
	<?php
	if(isset($_POST['Search']) AND $_POST[barang]<>'') {
		echo "<script>window.location=\"index.php?component=rekap_iklan&agen=".$_POST[agen]."&barang=".$_POST[barang]."&mulai=".$_POST[mulai]."&sampai=".$_POST[sampai]."\";</script>";
	}
	?>	</td>
  </tr>
  <?php if(isset($_GET[barang])) { ?>
  <tr>
    <td colspan="9">NAMA BARANG : <strong><?php echo $row_getagen['barang']; ?></strong></td>
  </tr>
  <tr>
    <th width="24"><strong>No</strong></th>
    <th width="189" align="center"><strong>Agen</strong></th>
    <th width="106" align="center"><strong>Subtotal</strong></th>
    <th width="72" align="center"><strong>Diskon</strong></th>
    <th width="83" align="center"><strong>Biaya Lain  </strong></th>
    <th width="76" align="center"><strong>Pajak</strong></th>
    <th width="117" align="center"><strong>Total</strong></th>
    <th width="112" align="center">Pembayaran</th>
    <th width="152" align="center">Saldo</th>
  </tr>
  <?php if($totalRows_data > 0) { ?>
  <?php $no=1; $jum =0; $saldo = 0; $dis=0; $ongk=0; $t_pjk=0; $byr=0; do { ?>
  <?php
  mysql_select_db($database_con_gl, $con_gl);
  $query_subtotal = "SELECT sum(gl_trans.total) as total FROM gl_trans, gl_rtrans WHERE gl_trans.pos='K' AND gl_trans.diskon<>'' AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_rtrans.kontak='$row_data[kontak]' AND gl_trans.barang='$_GET[barang]'";
  $subtotal = mysql_query($query_subtotal, $con_gl) or die(mysql_error());
  $row_subtotal = mysql_fetch_assoc($subtotal);
  $totalRows_subtotal = mysql_num_rows($subtotal);

//echo $query_subtotal;
mysql_select_db($database_con_gl, $con_gl);
$query_ongkir = "SELECT sum(gl_trans.total) as total, gl_rtrans.kontak FROM gl_trans, gl_rtrans WHERE gl_trans.pos='K' AND gl_trans.transaksi='Beban Biaya Lain' AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_rtrans.kontak='$row_data[kontak]' GROUP BY gl_rtrans.kontak";
$ongkir = mysql_query($query_ongkir, $con_gl) or die(mysql_error());
$row_ongkir = mysql_fetch_assoc($ongkir);
$totalRows_ongkir = mysql_num_rows($ongkir);

//echo $query_ongkir;
mysql_select_db($database_con_gl, $con_gl);
$query_pjk = "SELECT sum(gl_trans.total) as total, gl_rtrans.kontak FROM gl_trans, gl_rtrans WHERE gl_trans.pos='K' AND gl_trans.transaksi='Beban Pajak' AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_rtrans.kontak='$row_data[kontak]' GROUP BY gl_rtrans.kontak";
$pjk = mysql_query($query_pjk, $con_gl) or die(mysql_error());
$row_pjk = mysql_fetch_assoc($pjk);
$totalRows_pjk = mysql_num_rows($pjk);

mysql_select_db($database_con_gl, $con_gl);
$query_bayar = "SELECT sum(gl_trans.total) as total, gl_rtrans.kontak FROM gl_trans, gl_rtrans WHERE gl_trans.pos='K' AND gl_trans.x_ref <>'' AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_rtrans.kontak='$row_data[kontak]' GROUP BY gl_rtrans.kontak";
$bayar = mysql_query($query_bayar, $con_gl) or die(mysql_error());
$row_bayar = mysql_fetch_assoc($bayar);
$totalRows_bayar = mysql_num_rows($bayar);
//echo $query_bayar;
  $diskon = $row_data['total'] - $row_subtotal['total'];
  ?>
    <tr valign="top" bgcolor="#FFFFFF">
      <td align="center"  ><?php echo $no;$no++;?></td>
      <td ><?php echo $row_data['nama']; ?></td>
      <td align="right" ><?php echo number_format($row_subtotal['total'],0,',','.').",-"; $jum+=$row_subtotal['total']; ?></td>
      <td align="right" ><?php echo number_format($diskon,0,',','.').",-";$dis+=$diskon;?></td>
      <td align="right" bgcolor="#FFFFFF" ><?php echo number_format($row_ongkir['total'],0,',','.').",-"; $ongk +=$row_ongkir['total'];?></td>
      <td align="right" bgcolor="#FFFFFF" ><?php echo number_format($row_pjk['total'],0,',','.').",-"; $t_pjk += $row_pjk['total'];?></td>
      <td align="right" bgcolor="#FFFFFF" ><?php $xtotal = $row_data['total']+$row_ongkir['total']+$row_pjk['total'];echo number_format($xtotal,0,',','.').",-"; $saldo +=$xtotal;?></td>
      <td align="right" bgcolor="#FFFFFF" ><?php echo number_format($row_bayar['total'],0,',','.').",-"; $byr +=$row_bayar['total'];?></td>
      <td align="right" bgcolor="#FFFFFF" ><?php $tmpsaldo = $xtotal - $row_bayar['total'];echo number_format($tmpsaldo,0,',','.').",-";?></td>
    </tr>
    <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
	<tr valign="top">
          <td  >&nbsp;</td>
          <td align="right" ><strong>Total : </strong></td>
          <td align="right" ><?php echo number_format($jum,0,',','.').",-";?></td>
          <td align="right" ><?php echo number_format($dis,0,',','.').",-";?></td>
          <td align="right" ><?php echo number_format($ongk,0,',','.').",-";?></td>
          <td align="right" ><?php echo number_format($t_pjk,0,',','.').",-";?></td>
          <td align="right" ><?php echo number_format($saldo,0,',','.').",-";?></td>
          <td align="right" ><?php echo number_format($byr,0,',','.').",-";?></td>
          <td align="right" ><?php echo number_format($tmpsaldo,0,',','.').",-";?></td>
    </tr>
    <tr>
      <td colspan="9"  ><a href="include/print_rekapmajalah.php?agen=<?php echo $_GET[agen];?>&barang=<?php echo $_GET[barang];?>&mulai=<?php echo $_GET[mulai];?>&sampai=<?php echo $_GET[sampai];?>" onclick="NewWindow(this.href,'name','1080','500','yes');return false"><img src="images/_print.png" border="0" /> Cetak</a></td>
    </tr>
	<?php if($totalRows_data > $maxRows_data) { ?>
    <!--<tr>
      <td colspan="7"  ><table border="0" align="left">
          <tr>
            <td width="23%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, 0, $queryString_data); ?>"><img src="First.gif" border="0" /></a>
                <?php } // Show if not first page ?>            </td>
            <td width="31%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, max(0, $pageNum_data - 1), $queryString_data); ?>"><img src="Previous.gif" border="0" /></a>
                <?php } // Show if not first page ?>            </td>
            <td width="23%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, min($totalPages_data, $pageNum_data + 1), $queryString_data); ?>"><img src="Next.gif" border="0" /></a>
                <?php } // Show if not last page ?>            </td>
            <td width="23%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, $totalPages_data, $queryString_data); ?>"><img src="Last.gif" border="0" /></a>
                <?php } // Show if not last page ?>            </td>
          </tr>
        </table></td>
    </tr>-->
	<?php } ?>
   <?php }else{ ?>
  <tr bgcolor="#FFFFFF">
    <td colspan="9"  >Data tidak ada !!! </td>
  </tr>
  <?php } ?>
  <?php } ?>
</table>